using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using NetProtocol.POD;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_MailDetailPanel : UIBaseNode
    {

        #region Template Generate,don't modify
        protected partial class UIB_UIN_MailDetailPanel
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExText textTitle { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnClose { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textContent { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textSender { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textSendTime { protected set; get; }
            public UnityEngine.UI.ExLoopHorizontalScrollRect rewardLoopScrollRect { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnDel { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnGet { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textTitle", out var __tbv0);
                this.textTitle = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClose", out var __tbv1);
                this.btnClose = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textContent", out var __tbv2);
                this.textContent = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textSender", out var __tbv3);
                this.textSender = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textSendTime", out var __tbv4);
                this.textSendTime = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopHorizontalScrollRect>("rewardLoopScrollRect", out var __tbv5);
                this.rewardLoopScrollRect = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnDel", out var __tbv6);
                this.btnDel = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnGet", out var __tbv7);
                this.btnGet = __tbv7;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        protected UIB_UIN_MailDetailPanel ui { get; set; }

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_MailDetailPanel();
            ui.InitBinding(this.csObjBind);
        }

        LoopScrollAdapter<UIN_OneItem> loopRewardList;
        public MailSimplePOD mailSimplePOD { get; set; }
        public MailPOD mailPOD { get; private set; }
        protected override void OnInit()
        {
            this.loopRewardList = this.InitLoopScroll<UIN_OneItem>(this.ui.rewardLoopScrollRect, this.OnRewardChanged);
            this.ui.btnClose.AddClickEvent(OnBtnClose_Clicked);
            this.ui.btnGet.AddClickEvent(OnBtnGet_Clicked);
            this.ui.btnDel.AddClickEvent(OnBtnDelete_Clicked);

        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }
        protected override void OnDispose()
        {
            this.mailPOD = null;
            this.mailSimplePOD = null;
            this.loopRewardList = null;
        }
        public void Show(MailSimplePOD data)
        {
            this.mailSimplePOD = data;
            this.SendReadMail(this.mailSimplePOD).Forget();
        }


        async UniTaskVoid SendReadMail(MailSimplePOD mailSimplePOD)
        {
            var result = await MailModule.NetCore.CS_readMail(mailSimplePOD.id);
            if (result.IsFail)
            {
                return;
            }
            this.Show();
            this.mailPOD = result.mail;
            var cfg = TableCenter.mail.Get(this.mailSimplePOD.cid);
            mailSimplePOD.isRead = this.mailPOD.isRead;
            this.ui.textSender.text = string.Format(cfg.Sender, this.mailPOD.sender);
            var strContent = string.Empty;
            try
            {
                strContent = string.Format(cfg.Content, this.mailPOD.content.Split('|'));
            }
            catch (System.Exception)
            {
                IQIGame.Onigao.Framework.LogGame.LogError($"[mail]邮件内容填充错误，cfgId = {cfg.Id} Content={cfg.Content} 参数：{this.mailPOD.content}");
            }
            this.ui.textContent.text = strContent;
            this.ui.textTitle.text = string.Format(cfg.Title, this.mailPOD.title);
            this.ui.textSendTime.text = GameServerModule.Instance.GetDateTime(this.mailPOD.createTime).ToString("yyyy-MM-dd");
            this.RefreshPanel();
        }
        void OnBtnClose_Clicked()
        {
            this.Hide();
            var UIMail = this.belongWindow as UIMail;
            UIMail.MailListPanel.Show(false);
        }
        void OnBtnGet_Clicked()
        {
            if (!this.mailSimplePOD.canGetItem)
            {
                return;
            }
            this.SendPickUp().Forget();
        }
        void OnBtnDelete_Clicked()
        {
            if (this.mailSimplePOD.canGetItem)
            {
                return;
            }
            this.SendDeleteMail().Forget();
        }
        void RefreshPanel()
        {
            this.ui.btnDel.SetActive(!this.mailSimplePOD.canGetItem);
            this.ui.btnGet.SetActive(this.mailSimplePOD.canGetItem);
            this.loopRewardList.RefillCells(this.mailPOD.itemList.Count);
        }

        async UniTaskVoid SendPickUp()
        {
            List<long> list = ListPool<long>.Get();
            list.Add(this.mailPOD.id);
            var result = await MailModule.NetCore.CS_pickUp(list);
            ListPool<long>.Put(list);
            if (result.IsFail) return;
            this.mailPOD.isGotItem = true;
            this.mailSimplePOD.canGetItem = false;
            NoticeModule.ShowUIItemGet(result.getItems);
            this.RefreshPanel();
        }

        async UniTaskVoid SendDeleteMail()
        {
            List<long> list = ListPool<long>.Get();
            list.Add(this.mailPOD.id);
            var result = await MailModule.NetCore.CS_deleteMail(list);
            ListPool<long>.Put(list);
            if (result.IsFail) return;
            this.mailPOD.isGotItem = true;
            this.mailSimplePOD.canGetItem = false;
            //打开邮件列表界面
            var UIMail = this.belongWindow as UIMail;
            UIMail.MailListPanel.DeleteOne(this.mailPOD.id);
            this.Hide();
            UIMail.MailListPanel.Show(false);
        }

        void OnRewardChanged(UIN_OneItem item, int index)
        {
            var rwds = this.mailPOD.itemList[index];
            item.Show(rwds.cid, rwds.num);
            item.SetGray(this.mailPOD.isGotItem);
        }
    }
}
